package com.skd.mall.ware.service.impl;

import com.skd.common.constant.WareConstant;
import com.skd.mall.ware.dao.PurchaseDetailDao;
import com.skd.mall.ware.entity.PurchaseDetailEntity;
import com.skd.mall.ware.service.PurchaseDetailService;
import com.skd.mall.ware.service.WareSkuService;
import com.skd.mall.ware.vo.MergeVO;
import com.skd.mall.ware.vo.PurchaseDoneVO;
import com.skd.mall.ware.vo.PurchaseItemDoneVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skd.common.utils.PageUtils;
import com.skd.common.utils.Query;

import com.skd.mall.ware.dao.PurchaseDao;
import com.skd.mall.ware.entity.PurchaseEntity;
import com.skd.mall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
        wrapper.between("status",0,1);
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }
    @Autowired
    private PurchaseDetailService detailService;

    @Override
    public Integer merge(MergeVO mergeVO) {
        Long purchaseId = mergeVO.getPurchaseId();
        if(purchaseId == null){
            //新建采购单
            PurchaseEntity purchase = new PurchaseEntity();
            purchase.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            purchase.setCreateTime(new Date());
            purchase.setUpdateTime(new Date());
            this.save(purchase);
            purchaseId=purchase.getId();
        }

        final Long finalpurchaseId = purchaseId;
        //整合菜单需求单
        List<Long> items = mergeVO.getItems();
        List<PurchaseDetailEntity> list = items.stream().map(i -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setPurchaseId(finalpurchaseId);
            purchaseDetailEntity.setId(i);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGED.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());

        detailService.updateBatchById(list);
        //更新对应的采购单的更新时间
        PurchaseEntity purchase = new PurchaseEntity();
        purchase.setId(finalpurchaseId);
        purchase.setUpdateTime(new Date());
        this.updateById(purchase);
        return null;
    }

    @Autowired
    private PurchaseDetailDao detailDao;
    @Transactional
    @Override
    public void received(List<Long> ids) {
        // 1.领取的采购单的状态只能是新建或者已分配的采购单 其他的是不能领取的
        List<PurchaseEntity> purchaseEntities = ids.stream().map(i -> {
            return this.getById(i);
        }).filter(item->{
            if(item.getStatus()==WareConstant.PurchaseStatusEnum.CREATED.getCode() ||
            item.getStatus()==WareConstant.PurchaseStatusEnum.ASSIGED.getCode()){
                return true;
            }return false;
        }).map(item->{
            // 设置更新时间
            item.setUpdateTime(new Date());
            // 更新采购单的状态为 已领取
            item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            return item;
        }).collect(Collectors.toList());
        // 2.更新采购单的状态为 已领取
        this.updateBatchById(purchaseEntities);
        // 3.更新采购项的状态为 正在采购
        purchaseEntities.stream().forEach(purchaseEntity -> {
            List<PurchaseDetailEntity> list = detailDao.selectList(
                    new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", purchaseEntity.getId()));
            List<PurchaseDetailEntity> detailEntities = list.stream().map(item -> {
                item.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
                return item;
            }).collect(Collectors.toList());
            detailService.updateBatchById(detailEntities);
        });
    }

    @Autowired
    private WareSkuService wareSkuService;

    @Override
    public void done(PurchaseDoneVO vo) {
        // 获取采购单编号
        Long id = vo.getId();
        // 2.改变采购项的状态
        Boolean flag = true; // 记录采购的状态 默认为 完成
        // 获取所有的采购项
        List<PurchaseItemDoneVO> items = vo.getItems();
        List<PurchaseDetailEntity> list = new ArrayList<>();
        for (PurchaseItemDoneVO item : items) {
            PurchaseDetailEntity entity = new PurchaseDetailEntity();
            entity.setId(item.getItemId());
            //采购出现问题
            if(item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()){
                flag = false;
                entity.setStatus(item.getStatus());
            }else {
                //采购成功
                entity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
                //查出对应的采购详情
                // 跟进采购项编号查询出对应的采购项详情
                PurchaseDetailEntity detailEntity = detailService.getById(item.getItemId());
                //增加库存数量
                wareSkuService.addStock(detailEntity.getSkuId(),detailEntity.getWareId(),detailEntity.getSkuNum());

            }
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            detailEntity.setId(entity.getId());
            detailEntity.setStatus(entity.getStatus());
            list.add(detailEntity);
        }
        //更新采购需求状态
        detailService.updateBatchById(list);
        //更新采购单状态
        PurchaseEntity purchase = new PurchaseEntity();
        purchase.setId(vo.getId());
        purchase.setStatus(flag?
                WareConstant.PurchaseStatusEnum.FINISH.getCode()
                :WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        purchase.setUpdateTime(new Date());
        this.updateById(purchase);
    }

}